var getChinaTaxShaaxi = (function () {
    var __instance = null;
    
    function ChinaTaxShaanxi() {

        /**
         * 触发事件的几个场景
         * 1、网厅里点击退出会触发事件  data为空字符串
         * 2、登录成功后触发此事件      data为customerId
         * @param {*} data 
         */
        var event_callback = (data)=>{
            this.customerId = data;            
            if (this.customerId && this.jumpUrl) {
                this.jumpTo(this.jumpUrl);
            }
        }
        const chinatax_610000 = 'chinatax_610000';
        this.eventId = SyEvent.on('yqdz_610000', event_callback);
        this.customerId = null;
        this.jumpUrl = "";


        function wtOpened(token) {
            return new Promise((resolve, reject) => {
                SyAsynCall.execute(
                    {
                        uri: "tabExists",
                        data: JSON.stringify({
                            "token": token
                        }),
                        onSuccess: function (response) {
                            resolve(response)
                        },
                        onFailure: function () {
                            reject(response)
                        }
                    }
                )
            })
        }

        function openTab(username, password, nsrsbh, customerId) {
            var context = {
                reclassify: "N",
                customerId: customerId,
                yhxx: {
                    username: username,
                    password: password,
                    nsrsbh: nsrsbh
                }
            }
            requestId = SyRemote.invoke(
                {
                    uri: "/tax/chinatax/chrome/shaanxi/run",
                    data: JSON.stringify(context),
                    onSuccess: function (data) {

                    },
                    onFailure: function (code, message) {
                        console.log(code, message);
                    }
                }
            )
        }

        /**
         * 调用微服务接口在M2浏览器标签中打开网厅并登录
         * @param {*} username 
         * @param {*} password 
         * @param {*} nsrsbh 
         * @param {*} customerId 
         */
        this.loginWt = function (username, password, nsrsbh, customerId) {            
            //这里简单粗暴先关闭网厅标签页，标签页不存在会触发失败回调
            SyAsynCall.execute(
                {
                    uri: "closeTab",
                    data: JSON.stringify({
                        "token": chinatax_610000
                    }),
                    onSuccess: function (response) {
                        setTimeout(function(){
                            openTab(username, password, nsrsbh, customerId);
                        },100);
                    },
                    onFailure: function (code,message) {
                        openTab(username, password, nsrsbh, customerId);
                    }
                }
            )
        }
        /**
         * 内部接口,对于已登录企业，直接跳转至指定URL
         */
        this.jumpTo = function (url) {
            requestId = SyAsynCall.execute({
                uri: "openTab",
                data: JSON.stringify({
                    token: chinatax_610000,
                    url: url
                }),
                onSuccess: function (data) { console.log(data) },
                onFailure: function (errCode, errMsg) { console.log(errMsg + '. code: ' + errCode) }
            })
        }        

        /**
         * 供外部使用接口,打开网厅某个页面
         * 内部会自动判断是否相同企业是否网厅已登录
         */
        this.openUrl = function ({ username, password, nsrsbh, customerId, url }) {
            console.log(this.customerId, customerId);
            if (!this.customerId || this.customerId != customerId) {
                this.jumpUrl = url;
                this.loginWt(username, password, nsrsbh, customerId);
            } else {
                //需要判断Token标签是否存在,网厅登录后直接关闭标签页的场景下 customerId不会被清除
                wtOpened(chinatax_610000).then((data)=>{
                    var jo = JSON.parse(data);
                    if (jo.tabExists) {
                        console.log(this);
                        this.jumpTo(url);
                    }else{
                        this.customerId=null;
                        this.jumpUrl = url;
                        this.loginWt(username, password, nsrsbh, customerId);
                    }  
                });
                
            }
        }
    }
    return function () {
        if (!__instance) {
            __instance = new ChinaTaxShaanxi();
        }
        return __instance;
    }
})();



//调用演示
var sx = getChinaTaxShaaxi();

var data = {
    username: "916101133111557479",
    password: "U3dUZDJTaDg=",
    nsrsbh: "916101133111557479",
    customerId: 544238437330,
    url1: 'https://etax.shaanxi.chinatax.gov.cn/sbzs-cjpt-web/biz/sbzs/shbxfsb?gdslxDm=1',
    url:'https://etax.shaanxi.chinatax.gov.cn/xxmh/html/index_origin.html?gopage=true&m1=sbjs&m2=&fromWhere=&qxkzsx=1&tabTitle=wybsTab'
}
sx.openUrl(data);
